﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using DotNet.Business;
using DotNet.Model;
using DotNet.Common;
using System.Text;
using System.Collections.Generic;

public partial class Admin_EditNew : System.Web.UI.Page
{
    NewsBll newsBLL = new NewsBll();
    News news = new News();
    newClassBll nCBll = new newClassBll();
    protected void Page_Load(object sender, EventArgs e)
    {
         if (!IsPostBack)
        {
            if(Request.QueryString["state"]!=null)
            {
               updateNews();               
            }
            if (Request.QueryString["nid"]!=null)
            {
                
                loadNews(Convert.ToInt32(Request.QueryString["nid"].ToString())); 
            }
               
        }
       
    }
    #region 给HTML控件赋值---郑同飞
    /// <summary>
    /// 给HTML控件赋值---郑同飞
    /// </summary>
    /// <param name="nid">新闻id</param>
    public void loadNews(int nid)
    {
        news = newsBLL.GetModel(nid);
        if (news.newName.Contains("#"))
        {
            this.title_colorID.Value = news.newName.Substring(13, 7);
            this.newsNameID.Value = CommUtil.NoHTML(news.newName);
        }
        else
        {
            this.newsNameID.Value = news.newName;
        }

        this.FCKeditor1.Value = news.newInfo;
        ViewState["nid"] = news.newClassId.ToString();
        this.newSDateID.Value = news.newSDate.ToString();
        this.newEDateID.Value = news.newEDate.ToString();
        this.nTitleID.Value = news.nTitle;
        this.nKeyID.Value = news.nKey;
        this.nDescriptionID.Value = news.nDescription;
        this.nButtomID.Value = news.nButtom;
        this.HiddenId.Value = news.newId.ToString();

    }
    #endregion


    #region 更新新闻---郑同飞
    /// <summary>
    /// 更新新闻---郑同飞
    /// </summary>
    public void updateNews()
    {
            if (Request.Form["title_colorID"].ToString() != "")
            {
                news.newName = "<font color='" + Request.Form["title_colorID"].ToString() + "'>" + Request.Form["newsNameID"].ToString() + "</font>";
            }
            else
            {
                news.newName = Request.Form["newsNameID"].ToString();
            } 
            news.newClassId = Convert.ToInt32(Request.Form["selectDpID"].ToString());
            news.newInfo = Server.HtmlDecode(Request.Form["FCKeditor1"].ToString());
            news.newSDate = Convert.ToDateTime(Request.Form["newSDateID"].ToString());
            news.newEDate = DateTime.Now;
            news.nTitle = Request.Form["nTitleID"].ToString();
            news.nKey = Request.Form["nKeyID"].ToString();
            news.nDescription = Request.Form["nDescriptionID"].ToString();
            news.nButtom = Request.Form["nButtomID"].ToString();
            news.newId = Convert.ToInt32(Request.Form["HiddenId"].ToString());
            if (newsBLL.Update(news))
            {
                Response.Write("1");
                Response.End();
            }
    }
    #endregion


    #region 自定义下拉框---郑同飞
    /// <summary>
    /// 自定义下拉框---郑同飞
    /// </summary>
    /// <param name="name">控件id</param>
    /// <returns></returns>
    public string NewsClassDp(string name)
    {
        StringBuilder str = new StringBuilder();
        IList<newClass> list = nCBll.GetModel();
        str.Append("<select name='selectDpID' id='" + name + "' style='width:100px;'>");
        foreach (newClass nc in list)
        {
            if (nc.newClassId == Convert.ToInt32(ViewState["nid"].ToString()))
            {
                str.Append("<option value='" + nc.newClassId + "' selected='selected'>" + nc.newClassName + "</option>");

            }
            else
            {
                str.Append("<option value='" + nc.newClassId + "'>" + nc.newClassName + "</option>");
            }


        }
        str.Append("</select>");
        return str.ToString();
    }

    #endregion
}
